using System;
using System.Collections;
using System.Text;
//using crypto = TMGDevelopment.Security.CryptoText;
using System.Security.Cryptography;
using System.Configuration;

namespace Curriculum.DataAccess
{
	public sealed class Util
	{
		#region " Methods "

		/// <summary>
		/// encripta texto a MD5
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static byte[] CryptMD5(string valor)
		{
			byte[] hash;
			try
			{
				if (IsNullOrEmpty(valor))
				{
					return null;                    
				}
				MD5CryptoServiceProvider crypt = new MD5CryptoServiceProvider();
				ASCIIEncoding encoder = new ASCIIEncoding();                
				hash = crypt.ComputeHash(encoder.GetBytes(valor));
			}
			catch (Exception ex)
			{                
				throw ex;
			}
			return hash;
		}

		/// <summary>
		/// encripta texto a MD5
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string CryptMD5ToString(string valor)
		{
			byte[] hash = CryptMD5(valor);
			StringBuilder output = null;
			if (hash != null)
			{
				output = new StringBuilder(hash.Length);
				for (int i = 0; i < hash.Length - 1; i++)
				{
					output.Append(hash[i].ToString("X2"));
				} 
			}
			return (output != null) ? output.ToString() : null;            
		}

     
		private static bool IsNullOrEmpty(string valor)
		{
			return ( (valor == null) || (valor.Trim().Equals("")) );
		}


		#endregion methods
	}	
}
